package com.oa.lms.apps.actions.user;

import java.io.IOException;
import java.util.List;

import com.oa.lms.apps.actions.BasicAction;
import com.oa.lms.apps.business.user.IMailInfoService;
import com.oa.lms.apps.business.user.IPostInfoService;
import com.oa.lms.apps.business.user.ISectionInfoService;
import com.oa.lms.apps.business.user.IUserInfoService;
import com.oa.lms.apps.business.user.IUsersService;
import com.oa.lms.apps.constants.IDEDStatus;
import com.oa.lms.apps.entity.user.PostInfoValue;
import com.oa.lms.apps.entity.user.SectionInfoValue;
import com.oa.lms.apps.entity.user.UsersValue;

@SuppressWarnings("serial")
public class UsersAction extends BasicAction {
	
	private IUsersService usersService;
	private ISectionInfoService sectionInfoService;
	private IPostInfoService postInfoService;
	private IMailInfoService mailInfoService;
	private IUserInfoService userInfoService;
	private UsersValue usersInfo;
	
	private List<SectionInfoValue> sectionList;
	private List<PostInfoValue> postList;
	private List<UsersValue> userList;
	
	private String userId;
	private String userName;
	private String password;
	private String gender;
	private String age;
	private String phone;
	private String address;
	private String mailBox;
	private String sectionId;
	private String str;
	private String postId;
	private String mailId;
	private String infoId;
	
	public String addUser(){
		int mailId = mailInfoService.add(mailBox);
		int infoId = userInfoService.add(gender, Integer.parseInt(age), phone, address, mailId, Integer.parseInt(postId));
		password ="123456";
		int userId = usersService.add(userName, password, infoId);
		usersService.sendMail(userId);
		return SUCCESS;
	}
	public String toAdd(){
		sectionList = sectionInfoService.querySectionInfo(null, IDEDStatus.ENABLE);
		return SUCCESS;
	}
	
	
	public String updateUser(){
		mailInfoService.modify(Integer.parseInt(mailId),mailBox);
		userInfoService.modify(Integer.parseInt(infoId),gender, Integer.parseInt(age), phone, address, Integer.parseInt(postId));
		usersService.modify(Integer.parseInt(userId),userName, password);
		return SUCCESS;
		
	}
	

	public String toUpdate(){
		usersInfo = usersService.queryByUserId(Integer.parseInt(userId));
		gender = usersInfo.getUserInfo().getGender();
		sectionList = sectionInfoService.querySectionInfo(null, IDEDStatus.ENABLE);
		sectionId= usersInfo.getUserInfo().getPostInfo().getSectionInfo().getSectionId().toString();
		postList = postInfoService.queryPostInfo(null, Integer.parseInt(sectionId), IDEDStatus.ENABLE);
		postId = usersInfo.getUserInfo().getPostInfo().getPostId().toString();
		return SUCCESS;
	}
	
	public String toShowDetail(){
		usersInfo = usersService.queryByUserId(Integer.parseInt(userId));
		return SUCCESS;
		
	}
	public String toQuery(){
		totalpage = usersService.getTotalPageNumber(null, null, null, null, rows);
		userList = usersService.queryUsers(null, null, null, null, rows, currpage);
		return SUCCESS;
	}
	
	public String toDeleteUser(){
		usersService.delete(Integer.parseInt(userId));
		return SUCCESS;
	}
	
	public String toListUserSelf(){		
		usersInfo = (UsersValue) session.get("login_userInfo");
		return SUCCESS;
		
	}
	
	public String queryPostBySection() throws IOException{

		if (sectionId != null) {
			postList = postInfoService.queryPostInfo(null, Integer.parseInt(sectionId), null);
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			this.str = getPostOptionsFrom(postList);
			response.getWriter().print(str);
		}
		return null;
	}
	
	private String getPostOptionsFrom(List<PostInfoValue> list) {
		StringBuilder sb = new StringBuilder();
		sb.append("<option value=\"-1\">-请选择-</option>");
		for (PostInfoValue piv : list) {
			String temp = "<option value=\"" + piv.getPostId().toString()
					+ "\">" + piv.getPostName() + "</option>";
			sb.append(temp);
		}
		return sb.toString();
	}
	
	public String verifyMail() throws Exception{
		if(usersService.verifyMail(Integer.parseInt(userId)).equals(str)){
			usersInfo = usersService.queryByUserId(Integer.parseInt(userId));
			mailInfoService.activeMailBox(usersInfo.getUserInfo().getMailInfo().getMailId());
			return SUCCESS;
		}else
			return "failed";
	}
	

	public IUsersService getUsersService() {
		return usersService;
	}

	public void setUsersService(IUsersService usersService) {
		this.usersService = usersService;
	}
	public ISectionInfoService getSectionInfoService() {
		return sectionInfoService;
	}
	public void setSectionInfoService(ISectionInfoService sectionInfoService) {
		this.sectionInfoService = sectionInfoService;
	}
	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}
	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getMailBox() {
		return mailBox;
	}
	public void setMailBox(String mailBox) {
		this.mailBox = mailBox;
	}
	public String getSectionId() {
		return sectionId;
	}
	public void setSectionId(String sectionId) {
		this.sectionId = sectionId;
	}

	public IMailInfoService getMailInfoService() {
		return mailInfoService;
	}

	public void setMailInfoService(IMailInfoService mailInfoService) {
		this.mailInfoService = mailInfoService;
	}

	public IUserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(IUserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
	public List<UsersValue> getUserList() {
		return userList;
	}
	public void setUserList(List<UsersValue> userList) {
		this.userList = userList;
	}
	public UsersValue getUsersInfo() {
		return usersInfo;
	}
	public void setUsersInfo(UsersValue usersInfo) {
		this.usersInfo = usersInfo;
	}
	public IPostInfoService getPostInfoService() {
		return postInfoService;
	}
	public void setPostInfoService(IPostInfoService postInfoService) {
		this.postInfoService = postInfoService;
	}
	public List<PostInfoValue> getPostList() {
		return postList;
	}
	public void setPostList(List<PostInfoValue> postList) {
		this.postList = postList;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	public String getPostId() {
		return postId;
	}
	public void setPostId(String postId) {
		this.postId = postId;
	}
	public String getMailId() {
		return mailId;
	}
	public void setMailId(String mailId) {
		this.mailId = mailId;
	}
	public String getInfoId() {
		return infoId;
	}
	public void setInfoId(String infoId) {
		this.infoId = infoId;
	}



}
	

